#!/usr/bin/env groovy
package com.yidao.www

def autoBuild(project, branch, version){
    def calibrationBuildProfile = calibrationBuildProfile(project, branch, version)
    def label = calibrationBuildProfile.label
    def gitUrl = calibrationBuildProfile.gitUrl
    def buildScript = calibrationBuildProfile.buildScript
    def artifactDir = calibrationBuildProfile.artifactDir
    def artifactName = calibrationBuildProfile.artifactName
    def artifactory = calibrationBuildProfile.artifactory
    def cleanCmd = calibrationBuildProfile.cleanCmd
    def zipCmd = calibrationBuildProfile.zipCmd
    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, new Date())
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println calibrationBuildProfile
    println 'label: ' + label
    println 'gitUrl: ' + gitUrl
    println 'buildScript: ' + buildScript
    println 'artifactDir: ' + artifactDir
    println 'artifactName: ' + artifactName
    println 'artifactory: ' + artifactory
    println 'cleanCmd: ' + cleanCmd
    println '========================================================================================================'

    node(label){

        stage('checkout'){
            storageURL = srverUrl + '/' + deployTarget
            try{
                base.gitClone(gitUrl, branch)
            }catch(e){
                // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                println '代码克隆失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('package'){
            try{
                if(zipCmd){
                    base.batScriptStatus(zipCmd)
                }else{
                    base.zipPackage(artifactName, artifactDir)
                }
                // sh "tar zcvf ${artifactName} ${artifactDir}"
            }catch(e){
                println '[ERROR] 打包失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('upload'){
            print "artifactName: ${artifactName}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifactName, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('clean'){
            // def status = base.batScriptStatus(cleanCmd)
            // if(status != 0){
            //     println "[ERROR] 清理失败!"
            // }
            cleanWs()
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('retrun artifact'){
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            return deployTarget
        }

    }
}

def autoBuildWithVersion(project, branch, version){
    def calibrationBuildProfile = calibrationBuildProfile(project, branch, version)
    def label = calibrationBuildProfile.label
    def gitUrl = calibrationBuildProfile.gitUrl
    def buildScript = calibrationBuildProfile.buildScript
    def packageScript = calibrationBuildProfile.packageScript
    def artifactDir = calibrationBuildProfile.artifactDir
    def artifactName = calibrationBuildProfile.artifactName
    def artifactory = calibrationBuildProfile.artifactory
    def cleanCmd = calibrationBuildProfile.cleanCmd
    def date = new Date().format("yyyyMMdd")
    def calibrationBuildProfileSrc = calibrationBuildProfile(project, branch, date)
    def srcPackageScript = calibrationBuildProfileSrc.srcPackageScript
    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url

    println '============================================== BUILD INFO =============================================='
    println calibrationBuildProfile
    println 'label: ' + label
    println 'gitUrl: ' + gitUrl
    println 'buildScript: ' + buildScript
    println 'artifactDir: ' + artifactDir
    println 'artifactName: ' + artifactName
    println 'artifactory: ' + artifactory
    println 'cleanCmd: ' + cleanCmd
    println '========================================================================================================'

    node(label){

        stage('checkout'){
            def storageURL = srverUrl + '/' + deployTarget
            currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
            try{
                base.gitClone(gitUrl, branch)
            }catch(e){
                // notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                println '代码克隆失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('build'){
            try{
                def status = base.batScriptStatus(buildScript)
                if(status != 0){
                    currentBuild.result = 'FAILURE'
                    base.influxDb()
                    error '[ERROR] 编译失败!'
                }
            }catch(e){
                println '[ERROR] 编译失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('package'){
            try{
                base.batScriptStatus(srcPackageScript)
                base.batScriptStatus(packageScript)
                // sh "tar zcvf ${artifactName} ${artifactDir}"
            }catch(e){
                println '[ERROR] 打包失败!'
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('upload'){
            print "artifactName: ${artifactName}"
            print "deployTarget: ${deployTarget}"
            try{
                base.fileUpload(artifactName, deployTarget)
            }catch(e){
                println "[ERROR] 上传失败!"
                println e
                currentBuild.result = 'FAILURE'
                base.influxDb()
                error e.getMessage()
            }
        }

        stage('clean'){
            // def status = base.batScriptStatus(cleanCmd)
            // if(status != 0){
            //     println "[ERROR] 清理失败!"
            // }
            cleanWs()
        }

        stage('influxDb'){
            currentBuild.result = 'SUCCESS'
            base.influxDb()
        }

        stage('retrun artifact'){
            return deployTarget
        }

    }
}